import {familyCreate, familyUpdate} from '@/services/family/api';
import {ModalForm, ProForm, ProFormText} from '@ant-design/pro-components';
import {message} from 'antd';
import to from 'await-to-js';
import {type FC} from 'react';

type ReportGenerateModalProps = {
  modalVisible: boolean;
  onOpenChange: (open: boolean) => void;
  onSubmit: (familyInfo: FamilyCreateOrUpdateInfo) => void;
  familyInfo: FamilyCreateOrUpdateInfo;
};

export type FamilyCreateOrUpdateInfo = {
  id?: number;
  code?: string;
  name?: string;
  focus?: number;
  reportTypeEnum?: string;
};

const FamilyCreateModal: FC<ReportGenerateModalProps> = (props) => {
  const { familyInfo, modalVisible, onOpenChange, onSubmit } = props;

  const handleFamilyCreate = async (
    familyInfo: FamilyCreateOrUpdateInfo,
    callbackFun: (familyId: number | undefined) => void,
  ) => {
    if (!familyInfo || !familyInfo.name) {
      return message.error({
        content: '家庭名称不能为空',
      });
    }
    const [err, res] = await to(familyCreate(familyInfo));
    if (err || !res) {
      return message.error({
        content: err?.message || '家庭创建失败',
      });
    }
    const finalFamilyInfo = res.result;
    return message.success({
      content: `${finalFamilyInfo.name}(${finalFamilyInfo.code})创建成功`,
      onClose: () => {
        callbackFun(finalFamilyInfo.id);
      },
    });
  };

  const handleFamilyUpdate = async (
    familyInfo: FamilyCreateOrUpdateInfo,
    callbackFun: () => void,
  ) => {
    if (!familyInfo || !familyInfo.name) {
      return message.error({
        content: '家庭名称不能为空',
      });
    }
    const [err, res] = await to(familyUpdate(familyInfo));
    if (err || !res) {
      return message.error({
        content: err?.message || '家庭信息更新失败',
      });
    }
    return message.success({
      content: `${familyInfo.name}(${familyInfo.code})更新成功`,
      onClose: callbackFun,
    });
  };

  return (
    <ModalForm
      title={familyInfo.id ? `${familyInfo.name} - 家庭编辑` : '创建家庭'}
      open={modalVisible}
      onOpenChange={onOpenChange}
      width={600}
      onFinish={async (values) => {
        console.log('values', values);

        if (!familyInfo.id) {
          const _familyInfo: FamilyCreateOrUpdateInfo = { name: values.name };
          handleFamilyCreate(_familyInfo, (familyId) => {
            _familyInfo.id = familyId;
            onSubmit(_familyInfo);
          });
          return true;
        }

        const _familyInfo: FamilyCreateOrUpdateInfo = { id: familyInfo.id, code: familyInfo.code, name: values.name };
        handleFamilyUpdate(_familyInfo, () => {
          familyInfo.name = values.name;
          onSubmit(familyInfo);
        });
        return true;
      }}
      submitTimeout={2000}
      modalProps={{
        okText: '保 存',
        destroyOnClose: true,
      }}
      initialValues={familyInfo}
    >
      <ProForm.Group style={{ width: '100%' }}>
        <ProFormText name="name" label="家庭名称" placeholder="请输入家庭名称" />
        <ProFormText name="code" label="家庭码" placeholder="家庭码自动生成" disabled />
      </ProForm.Group>
    </ModalForm>
  );
};

export default FamilyCreateModal;
